package com.example.openapp;

import android.accessibilityservice.AccessibilityService;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import java.util.List;

/**
 * Created by 周涛 on 2017/8/8.
 */

public class MyAccessibilityService extends AccessibilityService {
    private static final String TAG = "MyAccessibilityService";
    public static final int SHUTDOWN_TYPE = 0x00001;//关机
    public static final int REBOOT_TYPE = 0x00002;//重启
    private static final int OPEN_SHUTDOWN_VIEW = 22;//打开关机重启界面
    private static final int CLICK_SHUTDOWN = 11;//点击关机
    private static final int CLICK_REBOOT = 44;//点击重启
    private static final int CLICK_CONFIRM = 33;//点击确定

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case OPEN_SHUTDOWN_VIEW://打开关机
                    performGlobalAction(GLOBAL_ACTION_POWER_DIALOG);
                    break;
                case CLICK_SHUTDOWN://点击关机
                    clickBtn("关机");
                    break;
                case CLICK_REBOOT://点击关机
                    clickBtn("重新启动");
                    break;
                case CLICK_CONFIRM://点击确定
                    clickBtn("确定");
                    break;
            }
        }
    };

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int type = intent.getIntExtra("type", 0);
        Log.d(TAG, "类型" + type);
        switch (type) {
            case SHUTDOWN_TYPE://关机
                handler.sendEmptyMessage(OPEN_SHUTDOWN_VIEW);
                handler.sendEmptyMessageDelayed(CLICK_SHUTDOWN, 1000);
                handler.sendEmptyMessageDelayed(CLICK_CONFIRM, 2000);
                break;
            case REBOOT_TYPE://重启
                handler.sendEmptyMessage(OPEN_SHUTDOWN_VIEW);
                handler.sendEmptyMessageDelayed(CLICK_REBOOT, 1000);
                handler.sendEmptyMessageDelayed(CLICK_CONFIRM, 2000);
                break;
        }
        return super.onStartCommand(intent, flags, startId);
    }

    private void clickBtn(String text) {
        AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
        if (nodeInfo != null) {
            List<AccessibilityNodeInfo> list = nodeInfo
                    .findAccessibilityNodeInfosByText(text);
            if (list != null && list.size() > 0) {
                for (AccessibilityNodeInfo n : list) {
                    n.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    n.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
            }
        }
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
    }

    @Override
    public void onInterrupt() {

    }

    @Override
    protected void onServiceConnected() {

    }
}
